/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.dialog.navigation.view;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import offset.nodes.client.dialog.navigation.model.RepositoryModel;
import offset.nodes.client.model.ServerModel;

/**
 *
 * @author
 * Walter
 * Lütgenau
 */
public class DebugRepositoryPanel extends NavigationApplet {
    class ConfiguredPathes {
        String[] pathes = new String[] {
             "/Project/Software development",
            "/Project",
            "/jcr:system/nodes/shortIds",
            "/documentation/nodes/Project/Software development"
       };
        int current = -1;
        
        public String getPath() {
            if (current < pathes.length - 1)
                current++;
            else
                current = 0;
            return pathes[current];
        }
    }
    
    protected ConfiguredPathes configuredPathes = new ConfiguredPathes();
    
    public void init() {
        ServerModel.setUserName("admin");
        ServerModel.setPassword("nodes");

        super.init();
    }
    
    public void initUI() {
        try {
            super.initUI();
            
            RepositoryPanel repositoryPanel = new RepositoryPanel();
            repositoryPanel.setServer(server);
            RepositoryModel repositoryModel = new DebugRepositoryModel(this, repositoryPanel, repository);
            
            JPanel panel = new JPanel();
            panel.setLayout(new BorderLayout());
            panel.add(repositoryPanel, BorderLayout.CENTER);
            
            JButton updateButton = new JButton("Update");
            updateButton.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    update();
                }
            });            
            
            panel.add(updateButton, BorderLayout.SOUTH);

            models.put(repositoryPanel.getCardName(), repositoryModel);
            navigationPanel.add(panel, repositoryPanel.getCardName());
            
            navigationPanel.showCard(repositoryPanel.getCardName());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    class DebugRepositoryModel extends RepositoryModel {
        public DebugRepositoryModel(Applet applet, RepositoryPanel panel, String repository) throws Exception {
            setApplet(applet);
            initPanel();
            setRepository(repository);
        }
        
        public String getPath() {
            return configuredPathes.getPath();
        }

        public void nodeSelected(String location) {
            System.out.println(location);
        }
    }

    public static void main(String[] arguments) {
        sun.applet.AppletViewer.main(new String[] {"src/test/java/offset/nodes/client/dialog/navigation/view/DebugRepositoryPanel.html"});
    }
    
}
